import org.dom4j.DocumentException;
import org.junit.Before;
import org.junit.Test;
import wsz.IUserDao;
import wsz.User;
import wsz.io.Resource;
import wsz.sqlSession.SqlSession;
import wsz.sqlSession.SqlSessionFactory;
import wsz.sqlSession.SqlSessionFactoryBuilder;

import java.beans.PropertyVetoException;
import java.io.InputStream;
import java.util.List;

/**
 * 采用getMapper调用
 * @author wsz
 * @date 2021/10/9 14:57
 **/
public class MapperTest {

    private SqlSession sqlSession;

    @Before
    public void init() throws PropertyVetoException, DocumentException {
        InputStream inputStream = Resource.getResourceAsStream("sqlMapConfig.xml");
        SqlSessionFactory factory = new SqlSessionFactoryBuilder().build(inputStream);
        sqlSession = factory.openSession();
    }

    @Test
    public void mapperQuery() throws Exception {

        IUserDao mapper = sqlSession.getMapper(IUserDao.class);

        List<User> all = mapper.findAll();
        System.out.println(all);

        User user = new User();
        user.setId(5);
        user.setName("222");

        List<User> byCondition1 = mapper.findByCondition(user);
        System.out.println(byCondition1);

        List<User> byCondition2 = mapper.findByCondition(user);
        System.out.println(byCondition2);

        User byId1 = mapper.findById(6);
        System.out.println(byId1);
        User byId2 = mapper.findById(6);
        System.out.println(byId2);
    }


    /**
     * 测试一级缓存
     * @throws Exception
     */
    @Test
    public void firstCache() throws Exception {
        IUserDao mapper = sqlSession.getMapper(IUserDao.class);

        User user = new User();
        user.setId(5);
        user.setName("222");

        List<User> byCondition1 = mapper.findByCondition(user);
        System.out.println(byCondition1);

        mapper.update(user);

        List<User> byCondition2 = mapper.findByCondition(user);
        System.out.println(byCondition2);
    }

    /**
     * 测试二级缓存
     * 二级缓存为mappedStatement级别，线程不安全
     * @throws Exception
     */
    @Test
    public void secondCache() throws Exception {
        IUserDao mapper1 = sqlSession.getMapper(IUserDao.class);
        IUserDao mapper2 = sqlSession.getMapper(IUserDao.class);

        User user = new User();
        user.setId(5);
        user.setName("222");

        List<User> byCondition1 = mapper1.findByCondition(user);
        System.out.println(byCondition1);

        List<User> byCondition2 = mapper2.findByCondition(user);
        System.out.println(byCondition2);

        /**
         * 准备执行sql:select * from user where id = ? and name = ?
         * [User(id=5, name=222, password=password)]
         * 从二级缓存中获取数据
         * [User(id=5, name=222, password=password)]
         */
    }


    @Test
    public void mapperInsert() {

        IUserDao mapper = sqlSession.getMapper(IUserDao.class);

        User user = new User();
        user.setName("222");
        user.setPassword("111");

        int count = mapper.insert(user);
        System.out.println(count);
    }

    @Test
    public void mapperUpdate() {

        IUserDao mapper = sqlSession.getMapper(IUserDao.class);

        User user = new User();
        user.setId(10);
        user.setName("10");
        user.setPassword("10");

        int count = mapper.update(user);
        System.out.println(count);
    }

    @Test
    public void mapperDelete() {

        IUserDao mapper = sqlSession.getMapper(IUserDao.class);

        User user = new User();
        user.setId(8);
        int count = mapper.deleteByUser(user);
        System.out.println(count);

        int count2 = mapper.deleteById(9);
        System.out.println(count2);

    }
}
